package controllers

import (
	"databaseStudy/models"
	"databaseStudy/models/globalValues"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
)

type CreateNewEmp struct {
	beego.Controller
}
type CreateNewDep struct {
	beego.Controller
}

func (c CreateNewEmp) Post() {
	token:=c.GetString("token")
	res:=globalValues.CheckToken(token)
	if !res{
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	emp := models.EmployeeInfo{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &emp); err != nil {
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	if err:=models.CreateNewEmp(&emp);err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	c.Ctx.ResponseWriter.WriteHeader(200)
}
func (c CreateNewDep) Post() {
	token:=c.GetString("token")
	res:=globalValues.CheckToken(token)
	if !res{
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	dep := models.DepartmentInfo{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &dep); err != nil {
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	if err:=models.CreateNewDep(&dep);err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	c.Ctx.ResponseWriter.WriteHeader(200)
}
